<template>


  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    destroy-on-close
    @close='closeDialog'
  >



      <el-tree
        :default-expanded-keys="expanded"
        :default-checked-keys="checked"
        :data="data"
        show-checkbox
        node-key="mark"
        accordion
        :props="defaultProps"
        ref="tree"
        :highlight-current="true"
      ></el-tree>



    <div slot="footer" class="dialog-footer">

      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submitFormCheck">确 定</el-button>
    </div>


  </el-dialog>

</template>

<script>
  import { assignResourcesToRole,searchUserRoleByUname} from "@/api/limits";
    import { tree } from "@/api/role";
    export default {
        name: "limits",
        data() {
            return {
                id:"",
                auto:[],
                checked: [],
                expanded: [],
                title:"权限分配",
              form:{


              },

                children:false,
                checkStrictly:"",
                listLoading:true,
                dialogFormVisible: false,
                data: [],
                defaultProps: {
                    children: 'childs',
                    label: 'name'
                }
            };
        },
      created() {
          tree().then(res =>{
              this.data=res.data
              console.log(res.data)

          })
        this.form.institutionNumber=localStorage.getItem("institutionNumber")
      },

      methods:{

          submitFormCheck(){
              this.listLoading=true

             let HalfCheckedKeys = this.$refs.tree.getHalfCheckedKeys()
             let CheckedKeys = this.$refs.tree.getCheckedKeys()
             console.log(HalfCheckedKeys,"HalfCheckedKeys",CheckedKeys,'CheckedKeys')
             this.form.marks=(HalfCheckedKeys.concat(CheckedKeys))

            assignResourcesToRole(this.form).then(res =>{

             console.log(res)
               if(res.code==200){
                   this.$baseMessage("分配成功");
                   this.dialogFormVisible=false

               }else {

               }
                this.listLoading = false;

            })
          },
            showEdit(row) {

                  this.expanded=[]
                  this.checked=[]
                     this.id=row.id
                    this.form.institutionNumber=row.institutionNumber
                    this.form.roleCode=row.roleCode
                    console.log(row)
                let institutionNumber=row.institutionNumber
                let roleCode=row.roleCode



                searchUserRoleByUname({institutionNumber,roleCode}).then(res =>{

                    if(res.data){
                        const result = []
                        var getId = arr => {
                            arr.forEach(v => {
                                if (v.children instanceof Array) {
                                    getId(v.children)
                                }else{
                                     result.push(v.name)
                                    this.expanded.push(v.name)
                                }
                            });
                        }

                        getId(res.data);

                        console.log(result) //["1-2", "2-1", "3"]
                        this.$refs.tree.setCheckedKeys(result)

                    }else {
                        this.$refs.tree.setCheckedKeys([])
                        this.expanded=[]
                        this.checked=[]
                    }





                })






                this.dialogFormVisible = true;
            },
          closeDialog(){
              this.expanded=[]
              this.$refs.tree.setCheckedKeys=[]


          },

          close(){
              this.checked=[]
            this.dialogFormVisible = false;
          }
        },
    }
</script>

<style scoped>

</style>
